﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Data;

namespace AutoCreateFolder
{
    public class Common
    {
        public List<DirectoryInfo> CreateCompanyFolder(DataSet ComapnyDS)
        {
            List<DirectoryInfo> CompanyDI = new List<DirectoryInfo>();
            string CurrentPath = Directory.GetCurrentDirectory();
            foreach (DataRow row in ComapnyDS.Tables[0].Rows)
            {
                string CurrentCompanyPath = CurrentPath + "\\" + row[0].ToString();
                //Console.WriteLine(CurrentCompanyPath);
                if (Directory.Exists(CurrentCompanyPath))
                {
                    CompanyDI.Add(new DirectoryInfo(CurrentCompanyPath));
                }
                else
                {
                    Console.WriteLine("Create Folder:" + CurrentCompanyPath);
                    CompanyDI.Add(Directory.CreateDirectory(CurrentCompanyPath));
                }
            }
            return CompanyDI;
        }
        public List<DirectoryInfo> CreateYearFolder(List<DirectoryInfo> CompanyDI)
        {
            List<DirectoryInfo> YearDI = new List<DirectoryInfo>();
            foreach (DirectoryInfo di in CompanyDI)
            {
                string CurrentYearPathStr = di.FullName + "\\" + DateTime.Now.Year.ToString();
                //Console.WriteLine(CurrentYearPathStr);
                if (Directory.Exists(CurrentYearPathStr))
                {
                    YearDI.Add(new DirectoryInfo(CurrentYearPathStr));
                }
                else
                {
                    Console.WriteLine("Create Folder:" + CurrentYearPathStr);
                    YearDI.Add(Directory.CreateDirectory(CurrentYearPathStr));
                }
            }
            return YearDI;
        }
        public List<DirectoryInfo> CreateMonthFolder(List<DirectoryInfo> YearDI)
        {
            List<DirectoryInfo> MonthDI = new List<DirectoryInfo>();
            foreach (DirectoryInfo di in YearDI)
            {
                string CurrentMonthPathStr = di.FullName + "\\" + (DateTime.Now.Month > 9 ? DateTime.Now.Month.ToString() : "0" + DateTime.Now.Month.ToString());
                //Console.WriteLine(CurrentMonthPathStr);
                if (Directory.Exists(CurrentMonthPathStr))
                {
                    MonthDI.Add(new DirectoryInfo(CurrentMonthPathStr));
                }
                else
                {
                    Console.WriteLine("Create Folder:" + CurrentMonthPathStr);
                    MonthDI.Add(Directory.CreateDirectory(CurrentMonthPathStr));
                }
            }
            return MonthDI;
        }
    }
}
